//
//  CZProgressImage.swift
//  weibo27
//
//  Created by mac on 16/4/23.
//  Copyright © 2016年 it.cast. All rights reserved.
//

import UIKit

class CZProgressImage: UIImageView {

    var progress: CGFloat = 0 {
        didSet{
            
             progressView.progress = progress
        }
    }
    
    init() {
        //调用一个没有指定参数的构造函数的时候，就要制定它的frame
       super.init(frame: CGRectZero)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    //设置控件UI
    private func setupUI(){
        
        progressView.backgroundColor = UIColor.clearColor()
        addSubview(progressView)
        
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    //懒加载控件
    private lazy var progressView:ProgressView = ProgressView()
}

private class ProgressView: UIView{
    
    var progress: CGFloat = 0 {
        didSet {
            // 重绘视图
            setNeedsDisplay()
        }
    }
    
    // rect == bounds
    override func drawRect(rect: CGRect) {
        
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        let r = min(rect.width, rect.height) * 0.5
        let start = CGFloat(-M_PI_2)
        let end = start + progress * 2 * CGFloat(M_PI)
        
        /**
         参数：
         1. 中心点
         2. 半径
         3. 起始弧度
         4. 截至弧度
         5. 是否顺时针
         */
        let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
        
        // 添加到中心点的连线
        path.addLineToPoint(center)
        path.closePath()
        
        UIColor(white: 1.0, alpha: 0.8).setFill()
        
        path.fill()

    }

}
















